package review.generate;

import java.io.File;
import java.io.FileWriter;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class ModelGenerator {

  public static void generate(String config, String template) throws Exception {
    Map<String, Object> mapConfig = Config.readModelConfig(config);
    String fileName = mapConfig.get("className").toString() + ".java";
    String packageName = mapConfig.get("packageName").toString();
    String url = Config.getBasePath() + "/src/main/java";
    File path = new File(url, packageName.replaceAll("[.]", "/"));
    File file = new File(path, fileName);
    File configPath = new File(Config.getBasePath(), "/target/classes/config");
    Configuration configuration = new Configuration(Configuration.VERSION_2_3_0);
    configuration.setDefaultEncoding("UTF-8");
    configuration.setDirectoryForTemplateLoading(configPath);
    Template tmp = configuration.getTemplate(template);
    FileWriter out = new FileWriter(file);
    tmp.process(mapConfig, out);
    out.close();
  }

}
